package dreamCookGame.puzzleMiniGame;

import dreamCookGame.Item;
import dreamCookGame.Scene;

public class PuzzleMiniGame extends Scene {
	Item[] _grid;
	Item[] _pieces;
	public Boolean _pieceSelected;
	Item _selectedPiece;
	String[] _placedPieces;
	public Boolean _win;
	
	public PuzzleMiniGame(String backgroundImgName)
	{
		super("puzzleMiniGame", backgroundImgName, null, 0, 0, null, null, null);
		String[] gridUpImage = {"upTriangle"};
		String[] gridDownImage = {"downTriangle"};
		String[] piece1 = {"puzzlePiece1"};
		String[] piece2 = {"puzzlePiece2"};
		String[] piece3 = {"puzzlePiece3"};
		String[] piece4 = {"puzzlePiece4"};
		String[] piece5 = {"puzzlePiece5"};
		String[] piece6 = {"puzzlePiece6"};
		String[] piece7 = {"puzzlePiece7"};
		String[] piece8 = {"puzzlePiece8"};
		String[] piece9 = {"puzzlePiece9"};
		String[] piece10 = {"puzzlePiece10"};
		String[] piece11 = {"puzzlePiece11"};
		String[] piece12 = {"puzzlePiece12"};
		String[] piece13 = {"puzzlePiece13"};
		String[] piece14 = {"puzzlePiece14"};
		String[] piece15 = {"puzzlePiece15"};
		String[] piece16 = {"puzzlePiece16"};
		_pieces = new Item[16];
		_grid = new Item[16];
		
		_grid[0] = new Item("Grid-1", gridUpImage, 331, 43, null, "triangleUp", null, false, null); 
		_grid[1] = new Item("Grid-2", gridUpImage, 273, 147, null, "triangleUp", null, false, null); 
		_grid[2] = new Item("Grid-3", gridDownImage, 333, 143, null, "triangleDown", null, false, null); 		
		_grid[3] = new Item("Grid-4", gridUpImage, 393, 147, null, "triangleUp", null, false, null); 
		_grid[4] = new Item("Grid-5", gridUpImage, 215, 253, null, "triangleUp", null, false, null); 
		_grid[5] = new Item("Grid-6", gridDownImage, 270, 245, null, "triangleDown", null, false, null); 
		_grid[6] = new Item("Grid-7", gridUpImage, 334, 245, null, "triangleUp", null, false, null); 
		_grid[7] = new Item("Grid-8", gridDownImage, 399, 246, null, "triangleDown", null, false, null); 
		_grid[8] = new Item("Grid-9", gridUpImage, 455, 252, null, "triangleUp", null, false, null); 
		_grid[9] = new Item("Grid-10", gridUpImage, 154, 355, null, "triangleUp", null, false, null);
		_grid[10] = new Item("Grid-11", gridDownImage, 213, 353, null, "triangleDown", null, false, null); 
		_grid[11] = new Item("Grid-12", gridUpImage, 273, 351, null, "triangleUp", null, false, null); 
		_grid[12] = new Item("Grid-13", gridDownImage, 332, 349, null, "triangleDown", null, false, null); 
		_grid[13] = new Item("Grid-14", gridUpImage, 395, 350, null, "triangleUp", null, false, null); 
		_grid[14] = new Item("Grid-15", gridDownImage, 459, 351, null, "triangleDown", null, false, null); 
		_grid[15] = new Item("Grid-16", gridUpImage, 514, 356, null, "triangleUp", null, false, null); 

		_pieces[0] = new Item("Piece-1", piece1, 500, 150, null, "triangleUp", null, false, null); 
		_pieces[1] = new Item("Piece-2", piece2, 650, 5, null, "triangleUp", null, false, null); 
		_pieces[2] = new Item("Piece-3", piece3, 200, 15, null, "triangleDown", null, false, null); 
		_pieces[3] = new Item("Piece-4", piece4, 250, 500, null, "triangleUp", null, false, null); 
		_pieces[4] = new Item("Piece-5", piece5, 20, 190, null, "triangleUp", null, false, null); 
		_pieces[5] = new Item("Piece-6", piece6, 450, 460, null, "triangleDown", null, false, null); 
		_pieces[6] = new Item("Piece-7", piece7, 115, 30, null, "triangleUp", null, false, null); 
		_pieces[7] = new Item("Piece-8", piece8, 425, 5, null, "triangleDown", null, false, null); 
		_pieces[8] = new Item("Piece-9", piece9, 120, 220, null, "triangleUp", null, false, null); 
		_pieces[9] = new Item("Piece-10", piece10, 655, 150, null, "triangleUp", null, false, null); 
		_pieces[10] = new Item("Piece-11", piece11, 535, 20, null, "triangleDown", null, false, null); 
		_pieces[11] = new Item("Piece-12", piece12, 660, 350, null, "triangleUp", null, false, null); 
		_pieces[12] = new Item("Piece-13", piece13, 35, 350, null, "triangleDown", null, false, null); 
		_pieces[13] = new Item("Piece-14", piece14, 30, 505, null, "triangleUp", null, false, null); 
		_pieces[14] = new Item("Piece-15", piece15, 650, 490, null, "triangleDown", null, false, null); 
		_pieces[15] = new Item("Piece-16", piece16, 5, 5, null, "triangleUp", null, false, null);  
		
		_itemsFront = new Item[_pieces.length];
		_itemsBack = new Item[_grid.length];
		
		for(int i = 0; i < _pieces.length; i++)
			_itemsFront[i] = _pieces[i];
		
		for(int i = 0; i < _grid.length; i++)
			_itemsBack[i] = _grid[i];
	
		_pieceSelected = false;
		_placedPieces = new String[_grid.length];
		_win = false;
	}
	
	public void selectPiece(int pieceNumber){
		_selectedPiece = _pieces[pieceNumber];
		_pieceSelected = true;
	}
	
	public void placeOnGrid(int gridPieceNumber){
		/*_selectedPiece._x = _grid[gridPieceNumber]._x;
		_selectedPiece._y = _grid[gridPieceNumber]._y;
		*/
		_selectedPiece.updatePosition(_grid[gridPieceNumber]._x, _grid[gridPieceNumber]._y);
		_pieceSelected = false;
		_placedPieces[gridPieceNumber] = _selectedPiece._name;
		checkIfWon();
	}
	
	public void checkIfWon(){
		if(_placedPieces[0] == "Piece-1")
			if(_placedPieces[1] == "Piece-2")
				if(_placedPieces[2] == "Piece-3")
					if(_placedPieces[3] == "Piece-4")
						if(_placedPieces[4] == "Piece-5")
							if(_placedPieces[5] == "Piece-6")
								if(_placedPieces[6] == "Piece-7")
									if(_placedPieces[7] == "Piece-8")
										if(_placedPieces[8] == "Piece-9")
											if(_placedPieces[9] == "Piece-10")
												if(_placedPieces[10] == "Piece-11")
													if(_placedPieces[11] == "Piece-12")
														if(_placedPieces[12] == "Piece-13")
															if(_placedPieces[13] == "Piece-14")
																if(_placedPieces[14] == "Piece-15")
																	if(_placedPieces[15] == "Piece-16")
																		_win = true;
	}
}
